package com.platform.rpc;

import com.platform.rpc.service.HelloWorld;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 13-1-16
 * Time: 下午2:58
 *
 */
public class WebServiceClient {

	public static void main(String[] args) {
		//dynamicClient();
		callHelloWorld();
	}

	public static void callHelloWorld() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.setServiceClass(HelloWorld.class);
		factory.setAddress("http://localhost:9000/helloWorld");
		HelloWorld client = (HelloWorld) factory.create();

		String reply = client.sayHi("HI");
		System.out.println("Server said: " + reply);
		System.exit(0);
	}

	public static void dynamicClient() {
		try {
			JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
			Client client = clientFactory.createClient("http://localhost:9000/helloWorld?wsdl");
			Object[] res = client.invoke("sayHi", "test echo");
			System.out.println("Echo response: " + res[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
